package com.feihong.txgw.utils;

import android.text.TextUtils;

import com.feihong.txgw.model.bean.user.CityInfo;
import com.feihong.txgw.model.bean.user.User;

/**
 * Created by Sabirjan on 2017/6/15.
 * TXGW.
 * 飞宏电子
 */

public class UserLoginInfo {

    private static final String CITY_NAME = "cityName";
    private static final String CITY_CODE = "cityCode";
    private static final String CITY_LOCATION_latitude = "latitude";
    private static final String CITY_LOCATION_longitude = "longitude";

    private static final String CITY_LOCATION_TIME = "locationtime";

    private static final String USER_PHONE = "userPhone";//手机号
    private static final String USER_ID = "userID";//用户ID
    private static final String USER_PIC = "userPIC";//头像
    private static final String USER_NAME = "userName";//姓名
    private static final String USER_SEX = "userSex";//性别
    private static final String USER_LOGIN_TIME = "userlogintime";//登录时间
    private static final String USER_TOKEN = "usertoken";//用户token
    private static final String USER_TOKEN_LAST_UPDATE = "tokenlastupdate";//最后更新token时间

    /**
     * 获取用户的登录状态
     *
     * @return true是已登录 ，false 是未登录
     */
    public static boolean isUserLogin() {

        String userPhone = PreferenceTools.getString(USER_PHONE, "");
        if (userPhone == null || userPhone.length() < 11) {
            return false;
        }
        return true;
    }

    /**
     * 获取用户的基本信息
     * @return
     */
    public static User getUserInfo() {
        if (!isUserLogin()) {
            return null;
        }
        User user = new User();
        user.setUserID(PreferenceTools.getString(USER_ID, ""));
        user.setUserName(PreferenceTools.getString(USER_NAME, ""));
        user.setUserPhone(PreferenceTools.getString(USER_PHONE, ""));
        user.setUserPic(PreferenceTools.getString(USER_PIC, ""));
        user.setUserSex(PreferenceTools.getString(USER_SEX, ""));

        return user;
    }
    /**
     * 获取当前用户ID
     * @return
     */
    public static String getUserId() {
        if (!isUserLogin()) {
            return "";
        }
        return PreferenceTools.getString(USER_ID, "");
    }
    /**
     * 获取当前用户的手机号
     *
     * @return
     */
    public static String getUserPhone() {
        if (!isUserLogin()) {
            return "";
        }
        return PreferenceTools.getString(USER_PHONE, "");
    }

    public static void setUserPhone(String newTel){
        PreferenceTools.putString(USER_PHONE, newTel);
    }
    public static String getUserToken() {
        return PreferenceTools.getString(USER_TOKEN, "");
    }
    /**
     * 保存用户的登录基本信息
     *
     * @param user 用户信息: 电话号码,登录时间,性别，积分，token。。。。。。
     * @return 保存成功返回true ,失败返回false
     */
    public static boolean saveUserLoginInfo(User user) {

        if (user == null || TextUtils.isEmpty(user.getUserPhone())) {
            return false;
        }

        PreferenceTools.putString(USER_PHONE, user.getUserPhone().isEmpty() ? UserLoginInfo.getUserPhone() : user.getUserPhone());
        PreferenceTools.putString(USER_ID,      user.getUserID().isEmpty() ? UserLoginInfo.getUserId() : user.getUserID());
        PreferenceTools.putString(USER_LOGIN_TIME, System.currentTimeMillis() + "");
        PreferenceTools.putString(USER_NAME, user.getUserName().isEmpty() ? UserLoginInfo.getUserInfo().getUserName() : user.getUserName());
        PreferenceTools.putString(USER_PIC, user.getUserPic().isEmpty() ? UserLoginInfo.getUserInfo().getUserPic() : ""+user.getUserPic());
        PreferenceTools.putString(USER_SEX, user.getUserSex().isEmpty() ? UserLoginInfo.getUserInfo().getUserSex() : ""+user.getUserSex());
        return true;
    }

    /**
     * 用户登录退出(本地保存的基本信息清理)
     *
     * @return 成功退出返回true，否则返回false
     */
    public static boolean userExitLogin() {
        PreferenceTools.putString(USER_ID, "");
        PreferenceTools.putString(USER_PHONE, "");
        PreferenceTools.putString(USER_LOGIN_TIME, 0 + "");
        PreferenceTools.putString(USER_NAME, "");
        PreferenceTools.putString(USER_SEX, "");
        PreferenceTools.putString(USER_PIC, "");
        return true;
    }

    /**
     * 更新用户的token信息
     * @param newtoken 新的token
     * @return 成功更新返回true，否则返回false
     */
    public static boolean updateUserToken(String newtoken) {

        PreferenceTools.putString(USER_TOKEN, newtoken);
        PreferenceTools.putString(USER_TOKEN_LAST_UPDATE, System.currentTimeMillis() + "");
        return true;
    }

    public static boolean setUserCityInfo(CityInfo cityInfo) {
        if (cityInfo == null || cityInfo.getCityName() == null || cityInfo.getCityCode() == null) {
            return false;
        }
        PreferenceTools.putString(CITY_NAME, cityInfo.getCityName());
        PreferenceTools.putString(CITY_CODE, cityInfo.getCityCode());
        PreferenceTools.putString(CITY_LOCATION_latitude, cityInfo.getLatitude());
        PreferenceTools.putString(CITY_LOCATION_longitude, cityInfo.getLongitude());
        PreferenceTools.putLong(CITY_LOCATION_TIME, System.currentTimeMillis());
        return true;
    }

    public static CityInfo getUserCityInfo()
    {
        CityInfo cityInfo = new CityInfo();
        cityInfo.setCityName(PreferenceTools.getString(CITY_NAME, ""));
        cityInfo.setCityCode(PreferenceTools.getString(CITY_CODE, ""));
        cityInfo.setPositionTime(PreferenceTools.getLong(CITY_LOCATION_TIME, 0));
        cityInfo.setLatitude(PreferenceTools.getString(CITY_LOCATION_latitude, ""));
        cityInfo.setLongitude(PreferenceTools.getString(CITY_LOCATION_longitude, ""));
        return cityInfo;
    }
}
